import { deleteTheme, getTheme, listTheme } from "@/api/service/theme/theme";
import { componentList, componentObj, currentComponent } from "../board/data/data";
import { resetForm, showForm, themForm } from "./form";
import { keys, query, table } from "./table";

export const list = async () => {
  table.value.loading = true;
  const data = await listTheme(query.value);
  table.value.data = data.rows;
  query.value.total = data.total;
  table.value.loading = false;
};

export const edit = async (id?: string) => {
  const targetId = id || keys.value[0];
  const data = await getTheme(`${targetId}`);
  themForm.value = data.data;
  componentList.value = data.data.image!;
  showForm.value = true;
};
export const create = async () => {
  componentList.value = [];
  currentComponent.value = {
    ...componentObj
  };
  resetForm();
  showForm.value = true;
};
export const del = async (id?: string) => {
  const ids: string[] = id ? [id] : keys.value;
  await deleteTheme(ids);
  await list();
  keys.value = [];
};

export const search = () => {
  query.value.pageNum = 1;
  list();
};
